/*
*  'collapseChange'    --->   切换侧边栏
*/

class EventBus {
  constructor() {
    this._events = this._events || new Object(); 
  }
}
EventBus.prototype.$on = function (eventName, callback) {
  if (typeof callback !== 'function') {
    throw new Error('callback is not a function')
  }
  if (this._events[eventName]) {
    this._events[eventName].push(callback)
  } else {
    this._events[eventName] = [callback]
  }
}
EventBus.prototype.$emit = function (eventName, ...args) {
  if (this._events[eventName]) {
    this._events[eventName].forEach((cb) => {
      cb(...args)
    })
  }
}
EventBus.prototype.$off = function (eventName, callback) {
  if (this._events[eventName]) {
    if (typeof callback !== 'function') {
      let ind = -1
      while (ind = this._events[eventName].indexOf(callback) >= 0) {
        this._events[eventName].splice(ind, 1)
      }
    } else {
      this._events[eventName] = []
    }
  }
}
const eventBus = new EventBus()
export default eventBus